package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.Province;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import clientView.Province_View.MouseListPr;
import database.ProvinceDelegate;

public class Province_Insert extends JDialog implements ActionListener{

	private JButton btnSave;
	private JButton btnCancel;
	private JLabel lblProTId;
	private JLabel lblProName;
	private JTextField txtProName;
	private JTextField txtProId;
	public Province_Insert( JFrame main)  {
		    super(main, "Thêm Tỉnh-Thành Phố", true);
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		    int height = 150;
		    int width = 420;
		    setSize(width, height);
		    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		    setResizable(false);
		    initComponet();
}
		public void initComponet(){
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblProTId = new JLabel("Mã Tỉnh-Thành Phố: ");
		lblProTId.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblProTName = new GridBagConstraints();
		gbc_lblProTName.anchor = GridBagConstraints.EAST;
		gbc_lblProTName.insets = new Insets(15, 15, 5, 5);
		gbc_lblProTName.gridx = 0;
		gbc_lblProTName.gridy = 0;
		getContentPane().add(lblProTId, gbc_lblProTName);
		
		txtProId = new JTextField();
		GridBagConstraints gbc_textField_1 = new GridBagConstraints();
		gbc_textField_1.gridwidth = 2;
		gbc_textField_1.anchor = GridBagConstraints.SOUTH;
		gbc_textField_1.insets = new Insets(0, 0, 5, 5);
		gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_1.gridx = 1;
		gbc_textField_1.gridy = 0;
		getContentPane().add(txtProId, gbc_textField_1);
		txtProId.setColumns(10);
		
		lblProName = new JLabel("Tên Tỉnh-Thành Phố :");
		lblProName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblProName = new GridBagConstraints();
		gbc_lblProName.anchor = GridBagConstraints.EAST;
		gbc_lblProName.insets = new Insets(0, 0, 5, 5);
		gbc_lblProName.gridx = 0;
		gbc_lblProName.gridy = 1;
		getContentPane().add(lblProName, gbc_lblProName);
		
		txtProName = new JTextField();
		txtProName.setEnabled(false);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 1;
		getContentPane().add(txtProName, gbc_textField);
		txtProName.setColumns(10);
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setCursor(UtilityGUI.cursorBtn);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnSave.setCursor(UtilityGUI.cursorBtn);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
		
		//add event ProId
		txtProId.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				ProvinceDelegate p = new ProvinceDelegate();
				if(StringUtil.isValidNumber(txtProId.getText())){
					if(!p.CheckInsertProId(StringUtil.removeUnwantedSpace(txtProId.getText()))||txtProId.getText()==""){
						txtProName.setEnabled(true);
					}else{
						txtProName.setEnabled(false);
					}
					
				}
				else{
					txtProName.setEnabled(false);
					
				}
			}
		});
		//add event Proname
		txtProName.addCaretListener(new CaretListener() {
			ProvinceDelegate p = new ProvinceDelegate();
			public void caretUpdate(CaretEvent arg0) {
				if(StringUtil.isValidTxtVN(txtProName.getText(), 3, 100)){
					if(p.CheckInsertProName(StringUtil.removeUnwantedSpace(txtProName.getText()))==false&&txtProName.getText().length()>3){
						btnSave.setEnabled(true);
					}else{

						btnSave.setEnabled(false);
					}
				}else{
					btnSave.setEnabled(false);
				}
			}
		});
	
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getSource() == btnSave){
				Province pro = new Province();
				String s = txtProId.getText();
				int intNum = Integer.parseInt (s);
				pro.setProvinceId(intNum);
				pro.setProvinceName(StringUtil.removeUnwantedSpace((txtProName.getText())));
				ProvinceDelegate proDelegate = new  ProvinceDelegate();
				boolean flag = proDelegate.insertProvince(pro, Main.userId);
				if(flag == true){
					this.setVisible(false);
					JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
				    UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
					Province_View.tblProV.setVisible(false);
					Province_View.tblProV =  UtilityGUI.getTable("Province", 1, "ProvinceId", true);
					Province_View. tblProV.addMouseListener(new MouseListPr()) ;	
					Province_View.scrollPane.setViewportView(Province_View.tblProV);
					dispose();
				}else{
					JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
							UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					return;
				}
			
		}else if(e.getSource() == btnCancel){
			dispose();
		}
		
	
		
	}

}
